package spider.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spider.entity.TbConfig;
import spider.service.SpiderService;

import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@Slf4j
@RequestMapping(value = "spider/")
public class SpiderController {

    @Autowired
    SpiderService spiderService;

    @ApiOperation(value = "开始爬取")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\"code\":\"200\",\"message\":\"成功\",\"data\":[]}"),
    })
    @GetMapping(value = "start")
    Map<String, Object> start() {
        return spiderService.start();
    }

    @ApiOperation(value = "修改配置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\"code\":\"200\",\"message\":\"成功\",\"data\":[]}"),
    })
    @PostMapping(value = "updateConfig")
    Map<String, Object> updateConfig(@RequestBody TbConfig body) {
        return spiderService.updateConfig(body);
    }

    @ApiOperation(value = "添加配置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\"code\":\"200\",\"message\":\"成功\",\"data\":[]}"),
    })
    @PostMapping(value = "addConfig")
    Map<String, Object> addConfig(@RequestBody TbConfig body) {
        return spiderService.addConfig(body);
    }

    @ApiOperation(value = "查询所有配置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\"code\":\"200\",\"message\":\"成功\",\"data\":[]}"),
    })
    @GetMapping(value = "showConfig")
    Map<String, Object> showConfig() {
        return spiderService.showConfig();
    }

    @ApiOperation(value = "根据id查询配置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\"code\":\"200\",\"message\":\"成功\",\"data\":[]}"),
    })
    @GetMapping(value = "selectConfigById")
    Map<String, Object> selectConfigById(@RequestParam("id") String id) {
        return spiderService.selectConfigById(id);
    }

    @ApiOperation(value = "删除配置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\"code\":\"200\",\"message\":\"成功\",\"data\":[]}"),
    })
    @PostMapping(value = "deleteConfig")
    Map<String, Object> deleteConfig(@RequestParam("id") String id) {
        return spiderService.deleteConfig(id);
    }

}
